package it.stasbranger.myproject.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;

import com.google.inject.Inject;

import it.stasbranger.myproject.client.managed.request.ApplicationEntityTypesProcessor;
import it.stasbranger.myproject.client.managed.request.ApplicationRequestFactory;
import it.stasbranger.myproject.client.managed.request.RequestsProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersHasRequestsProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersProxy;

import it.stasbranger.myproject.client.scaffold.place.ProxyPlace;

public class ApplicationDetailsActivities implements ActivityMapper {
	/**
	 */
	private final ApplicationRequestFactory requests;

	/**
	 */
	private final PlaceController placeController;

	/**
	 */
	@Inject
	public ApplicationDetailsActivities(ApplicationRequestFactory requestFactory, PlaceController placeController) {
		this.requests = requestFactory;
		this.placeController = placeController;
	}

	public Activity getActivity(Place place) {
		if (!(place instanceof ProxyPlace)) {
			return null;
		}

		final ProxyPlace proxyPlace = (ProxyPlace) place;

		return new ApplicationEntityTypesProcessor<Activity>() {

			@Override
			// Suppliers
			public void handleSuppliers(SuppliersProxy proxy) {
				setResult(new SuppliersActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}

			@Override
			// Requests
			public void handleRequests(RequestsProxy proxy) {
				setResult(new RequestsActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}

			@Override
			// SuppliersHasRequests
			public void handleSuppliersHasRequests(SuppliersHasRequestsProxy proxy) {
				setResult(new SuppliersHasRequestsActivitiesMapper(requests, placeController).getActivity(proxyPlace));
			}
		}.process(proxyPlace.getProxyClass());
	}
}
